package com.example.cinemaorder.mapper;

import com.example.cinemaorder.entity.SysBill;
import com.example.cinemaorder.entity.vo.SearchMovieVo;
import org.apache.ibatis.annotations.*;

/**
 * @author 高垚淼
 * @version 1.0
 */
@Mapper
public interface SysBillMapper {

    @Insert("insert into sys_bill values(null,#{userId},#{sessionId},#{payState},#{cancelState},#{seats}," +
            "#{createTime},#{deadline},#{cancelTime},#{price})")
    @Options(useGeneratedKeys = true,keyProperty ="billId",keyColumn = "bill_id")
    void insertBill(SysBill sysBill);

    @Select("select * from sys_bill where bill_id = #{billId}")
    SysBill getBillById(Long billId);

    @Select("select * from sys_cinema_arrange where session_id = #{sessionId}")
    SearchMovieVo getSessionById(Long sessionId);

    @Select("select * from sys_cinema_arrange sc inner join sys_bill sb on sc.session_id = sb.session_id " +
            "where bill_id = #{id}")
    SearchMovieVo getCinemaInfo(Long id);

    @Select("select movie_name from sys_bill sb inner join sys_cinema_arrange sca on sb.session_id = sca.session_id " +
            " inner join sys_movie sm on sca.movie_id = sm.movie_id where sb.bill_id = #{id} ")
    Object movieName(Long id);

    @Select("select * from sys_bill where bill_id = #{id}")
    SysBill bill(Long id);

    @Select("select * from sys_bill where seats = #{seats}")
    SysBill getBillBySeats(String seats);

    @Update("update sys_bill set cancel_state = 1 where bill_id = #{billId}")
    void updateCancelState(Long billId);

    @Update("update sys_bill set cancel_time = #{formatDate} where bill_id = #{id}")
    void updateCancelTime(@Param("id") Long id, @Param("formatDate") String formatDate);

    @Update("update sys_bill set pay_state = 1 ,price = #{price} where bill_id = #{billId}")
    void payBill(SysBill sysBill);

    @Delete("delete from sys_bill where pay_state =0 and cancel_state = 0 and cancel_time is null")
    void clearInvalidOrder();
}
